package ru.ivanovak.aptu.ccheckers;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.Canvas;

public class Field {
	
	private static final int S = 0;
	private static final int C = 1;
	private static final int D = 2;
	
	private List<List<GameObject>> gameMatrix = 
			new ArrayList<List<GameObject>>();
	
	public static class Space extends GameObject {
		@Override
		public void onDraw(Canvas c) {
		}
		@Override
		public void poked(int i, int j) {
		}
		@Override
		public void redirect(GameObject go) {
			go.accept(this);
		}
	}
	
	public static class DeadZone extends GameObject {
		@Override
		public void onDraw(Canvas c) {
		}
		@Override
		public void poked(int i, int j) {
		}
		@Override
		public void redirect(GameObject go) {
			go.accept(this);
		}
	}
	
	private final int initField[][] = {
			{D, D, D, C, C, C, D, D, D}, 
			{D, D, D, C, C, C, D, D, D}, 
			{D, D, D, C, C, C, D, D, D}, 
			{C, C, C, C, C, C, C, C, C},
			{C, C, C, C, S, C, C, C, C},
			{C, C, C, C, C, C, C, C, C},
			{D, D, D, C, C, C, D, D, D}, 
			{D, D, D, C, C, C, D, D, D}, 
			{D, D, D, C, C, C, D, D, D} 
	};
	
	public Field(GameView gv, Bitmap coinBmp, Bitmap selectedCoinBmp) {
		for (int i = 0; i < 9; ++i) {
			gameMatrix.add(new ArrayList<GameObject>());
			for (int j = 0; j < 9; ++j) {
				if (initField[i][j] == C) {
					gameMatrix.get(i).add(
							new Coin(gv, 
									coinBmp,
									selectedCoinBmp,
									i, 
									j,
									gameMatrix));
				} else if (initField[i][j] == D) {
					gameMatrix.get(i).add(new DeadZone());
				} else {
					gameMatrix.get(i).add(new Space());
				}
			}
		}
	}
	
	public void poke(int i, int j) {
		if (i >= 0 && i < 9 && j >= 0 && j < 9) {
			for (List<GameObject> ls : gameMatrix) {
				for (GameObject go : ls) {
					go.poked(i, j);
				}
			}
		}
	}
	
	public void onDraw(Canvas canvas) {
		for (List<GameObject> ls : gameMatrix) {
			for (GameObject go : ls) {
				go.onDraw(canvas);
			}
		}
	}
}
